package com.koron.plan.web.maintainplan.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.plan.web.common.CommonUtils;
import com.koron.plan.web.maintainplan.bean.entity.MaintainPlanStandardBean;
import com.koron.plan.web.maintainplan.mapper.MaintainPlanStandardMapper;
import com.koron.plan.web.maintainplan.service.MaintainPlanStandardService;
import org.apache.commons.lang.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author twcao
 * @title: MaintainPlanStandardServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 计划标准service实现
 * @date 2021/9/28:56
 */
@Service
public class MaintainPlanStandardServiceImpl implements MaintainPlanStandardService {

    /**
     * 保存或更新
     * @param factory 会话
     * @param beans   待保存的集合
     * @return 是否保存成功
     */
    @Override
    public boolean saveOrUpdate(SessionFactory factory, List<MaintainPlanStandardBean> beans) {
        if(CollectionUtils.isEmpty(beans)) {
            return true;
        }
        MaintainPlanStandardMapper standardMapper = factory.getMapper(MaintainPlanStandardMapper.class);
        beans.stream().forEach(row -> {
            CommonUtils.update(row);
            if(StringUtils.isBlank(row.getId())) {
                row.setId(CodeTools.getCode32());
            }
        });
        return standardMapper.saveOrUpdate(beans);
    }

    /**
     * 保存或更新
     *
     * @param factory 会话
     * @param planId  计划id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteByPlanId(SessionFactory factory, String planId) {
        if(StringUtils.isBlank(planId)) {
            return true;
        }
        MaintainPlanStandardMapper standardMapper = factory.getMapper(MaintainPlanStandardMapper.class);
        return standardMapper.deleteByPlanId(planId) > 0;
    }
}
